from flask import Flask
from flask import request, render_template, jsonify, Response

from spark_chat import chat

app = Flask(__name__)


@app.get("/")
def index():
    return render_template(r"index.html")


@app.post("/coderv")
def coderv():
    code = request.json.get("code")
    def ext_gen(code):
        for g in chat(code):
            yield g.content
    return Response(ext_gen(code), mimetype="text/event-stream")


@app.route("/stream_text")
def stream_text():
    def generate_text():
        for i in range(99999999):
            yield f"Line {i}\t"
            import time
            time.sleep(0.2)
            # 这里可以有IO操作或者其他耗时任务
            # 使用sleep模拟耗时
            # import time
            # time.sleep(1)]
    return Response(generate_text(), mimetype="text/event-stream")


if __name__ == "__main__":
    app.run(debug=True)
